package com.wtgroup.demo.leetcode.q007_整数反转;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/3/23 11:12
 */
public class S1 {

    static int INT_MAX = 0x7fffffff;
    static int INT_MIN = 0x80000000;
    static int POP_THRESHOLD_P = INT_MAX % 10;
    static int POP_THRESHOLD_N = INT_MAX % 10;
    // 2147483647 / 10
    static int MAX_REV = INT_MAX / 10;
    // -2147483648 / 10
    static int MIN_REV = INT_MIN / 10;

    /**
     * 1. /% 模拟栈;
     * 2. 溢出判断技巧.
     */
    public int reverse(int x) {
        int rev = 0;
        while (x!=0) {
            int pop = x % 10;
            if(rev > MAX_REV || (rev==MAX_REV && pop > 7)) return 0;
            if(rev < MIN_REV || (rev==MIN_REV && pop < -8)) return 0;

            rev = rev * 10 + pop;

            x = x / 10;
        }

        return rev;
    }

    public static void main(String[] args) {


        // int num = 453468;
        // int num = -674586;
        // int num = 2147483647;
        int num = 1534236469;
        System.out.println(new S1().reverse(num));
    }

}
